package com.alstudio.net.protocol.xmpp;

import java.util.HashMap;

import com.alstudio.net.protocol.xmpp.packact.IQ;
import com.alstudio.net.protocol.xmpp.packact.IQNameSpace;
import com.alstudio.net.protocol.xmpp.packact.Message;
import com.alstudio.net.protocol.xmpp.packact.Presence;
import com.alstudio.net.protocol.xmpp.parser.iq.FeedBackIQPackactParser;
import com.alstudio.net.protocol.xmpp.parser.iq.IQPackageCallback;
import com.alstudio.net.protocol.xmpp.parser.iq.XmppPingPackactParser;
import com.alstudio.net.protocol.xmpp.parser.message.MessageParser;
import com.alstudio.net.protocol.xmpp.parser.presence.PresenceParser;
import com.alstudio.utils.log.Mylog;

public class XmppParseConfigue {

	private HashMap<String, IQPackageCallback> map = new HashMap<String, IQPackageCallback>();

	public XmppParseConfigue() {
		registerIQParseMethod();
	}

	/**
	 * 注册指定命名空间的IQ解析器
	 */
	public void registerIQParseMethod() {
		map.put(IQNameSpace.NS_SUGGEST, new FeedBackIQPackactParser() {
		});

		map.put(IQNameSpace.NS_URN_XMPP_PING, new XmppPingPackactParser() {
		});
	}

	public void registerMessageParseMethod() {

	}

	public void registerPresenceParseMethod() {

	}

	public synchronized void getIQParser(IQ packact, String stream,
			XmppListener listener) {
		if (map.get(packact.getNameSpace()) != null) {
			IQPackageCallback callback = map.get(packact.getNameSpace());
			Mylog.d("开始解析" + packact.getNameSpace());
			try {
				callback.parseIQPackage(packact, stream, listener);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public synchronized void getMessageParser(Message msg, String stream,
			XmppListener listener) {
		MessageParser parser = new MessageParser(msg, stream, listener);
		parser.startParseMessage();
	}

	public synchronized void getPresenceParser(Presence presence,
			String stream, XmppListener listener) {
		PresenceParser parser = new PresenceParser(presence, stream, listener);
		parser.startParsePresence();
	}

}
